/*
 * @brief : easyflash库的移植接口文件  
 * @date :  2022-02-xx
 * @version : v1.0.0
 * @copyright(c) 2020 : OptoMedic company Co.,Ltd. All rights reserved
 * @Change Logs:   
 * @date         author         notes:  
 */
/*
 * This file is part of the EasyFlash Library.
 *
 * Copyright (c) 2015-2019, Armink, <armink.ztl@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Function: Portable interface for each platform.
 * Created on: 2015-01-16
 */

#include <easyflash.h>
#include <stdarg.h>
#include "bsp_w25qxx.h"
#include "stdlib.h"
#include "stdint.h"
#include "string.h"
#include "slog.h"
#include "rtthread.h"

rt_mutex_t spiflash_lock = RT_NULL;

/* default environment variables set for user */
static const ef_env default_env_set[] = {
    {"boot_times", "1", 0},
};

/**
 * Flash port for hardware initialize.
 *
 * @param default_env default ENV set for user
 * @param default_env_size default ENV size
 *
 * @return result
 */
EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size) {
    EfErrCode result = EF_NO_ERR;

    *default_env = default_env_set;
    *default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);

    spiflash_lock = rt_mutex_create("spiflashlock", RT_IPC_FLAG_PRIO);   //  RT_IPC_FLAG_FIFO
    if (spiflash_lock == RT_NULL) 
    {
        printf("spiflashlock failed!\r\n");
    }

    return result;
}

/**
 * Read data from flash.
 * @note This operation's units is word.
 *
 * @param addr flash address
 * @param buf buffer to store read data
 * @param size read bytes size
 *
 * @return result
 */
EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size) {
    EfErrCode result = EF_NO_ERR;

    /* You can add your code under here. */
    W25QXX_Read((uint8_t *)buf, addr, size);

    return result;
}

/**
 * Erase data on flash.
 * @note This operation is irreversible.
 * @note This operation's units is different which on many chips.
 *
 * @param addr flash address
 * @param size erase bytes size
 *
 * @return result
 */
EfErrCode ef_port_erase(uint32_t addr, size_t size) {
    EfErrCode result = EF_NO_ERR;

    /* make sure the start address is a multiple of EF_ERASE_MIN_SIZE */
    EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);

    /* You can add your code under here. */
    (void)size;
//    W25QXX_Erase_Sector(addr);
    W25QXX_EraseSector(addr);

    return result;
}
/**
 * Write data to flash.
 * @note This operation's units is word.
 * @note This operation must after erase. @see flash_erase.
 *
 * @param addr flash address
 * @param buf the write data buffer
 * @param size write bytes size
 *
 * @return result
 */
EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size) {
    EfErrCode result = EF_NO_ERR;
    
    /* You can add your code under here. */
    
    W25QXX_Write_NoCheck((uint8_t *)buf, addr, size);
    
    return result;
}

/**
 * lock the ENV ram cache
 */
void ef_port_env_lock(void) {
    
    if (spiflash_lock) 
        rt_mutex_take(spiflash_lock, RT_WAITING_FOREVER);
    
    /* You can add your code under here. */
}

/**
 * unlock the ENV ram cache
 */
void ef_port_env_unlock(void) {
    
    /* You can add your code under here. */
    if (spiflash_lock)
        rt_mutex_release(spiflash_lock);

}


/**
 * This function is print flash debug info.
 *
 * @param file the file which has call this function
 * @param line the line number which has call this function
 * @param format output format
 * @param ... args
 *
 */
void ef_log_debug(const char *file, const long line, const char *format, ...) {

#ifdef PRINT_DEBUG

    va_list args;

    /* args point to the first variable parameter */
    va_start(args, format);

    /* You can add your code under here. */
    
    va_end(args);

#endif

}

/**
 * This function is print flash routine info.
 *
 * @param format output format
 * @param ... args
 */
void ef_log_info(const char *format, ...) {
    char log_buf[128];
    
    va_list args;

    /* args point to the first variable parameter */
    va_start(args, format);

    /* You can add your code under here. */
    ef_print("[Flash]");
    /* must use vprintf to print */
    vsprintf(log_buf, format, args);
    ef_print("%s", log_buf);
    printf("\r");
    
    va_end(args);
}
/**
 * This function is print flash non-package info.
 *
 * @param format output format
 * @param ... args
 */
void ef_print(const char *format, ...) {
    
    // char log_buf[128];
    
    // va_list args;

    // /* args point to the first variable parameter */
    // va_start(args, format);
    
	// vsprintf(log_buf, format, args);
    // printf("%s", log_buf);
    // /* You can add your code under here. */
    
//    va_end(args);
}


void EasyFlash_Test_ENV(void) 
{
    uint32_t i_boot_times = 0;
    char value[32] = {0};
    size_t len;
    /* get the boot count number from Env */
    len = ef_get_env_blob("boot_times", value, sizeof(value), NULL);
    
    SLOG_PRINT("len %d\r\n", len);   

    i_boot_times = atol(value);
    
    SLOG_PRINT("boot_times %d\r\n", i_boot_times);
    /* boot count +1 */
    i_boot_times++;
    
    sprintf(value, "%d", i_boot_times);
    
    SLOG_PRINT("value %s\r\n", value);
    /* set and store the boot count number to Env */
//        ef_set_env("boot_times", c_new_boot_times);
    
    EfErrCode err = ef_set_env_blob("boot_times", value, strlen(value));
    SLOG_PRINT("err %x\r\n", err);
}

